package com.wawei.seeds;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

import com.igexin.sdk.PushManager;
import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.services.WalnutRegAuthServiceHandler;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import retrofit.RestAdapter;

/**
 *
 * Created by t27891 on 15/7/3.
 */
public class WalnutSpashActivity extends ActionBarActivity {
    private static final int SCHEDULER_THREAD = 1;
    private static final int SPLASH_PERIOD = 3;

    private static int login_count = 0;

    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(SCHEDULER_THREAD);
    private ScheduledFuture future;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_splash);
        initTimer();
        PushManager.getInstance().initialize(this.getApplicationContext());
    }


    private void initTimer(){
        final Runnable worker = new Runnable() {
            @Override
            public void run() {
                if(login_count == 0) {
                    login_count ++;
                    HeroInfo heroInfo = new HeroInfo();
                    String userPassword = PrefUtils.getUserPassword(WalnutSpashActivity.this);
                    if(!userPassword.equals("|||")) {
                        heroInfo.userName = PrefUtils.getPrefUser(WalnutSpashActivity.this);
                        heroInfo.password = PrefUtils.getPrefPassword(WalnutSpashActivity.this);

                        RestAdapter restAdapter = RetrofitNetworkService.makeRestAdapter();
                        WalnutRegAuthServiceHandler serviceHandler = new WalnutRegAuthServiceHandler();

                        try {
                            heroInfo = serviceHandler.login(restAdapter, heroInfo);
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    future.cancel(true);
                    if (heroInfo != null && null != heroInfo.loginToken) {
                        SeedsApp app = (SeedsApp) WalnutSpashActivity.this.getApplication();
                        app.getSession().put(SeedsApp.LOGIN_USER, heroInfo);
                        PrefUtils.markUserPassword(WalnutSpashActivity.this, heroInfo.userName, heroInfo.password);
                        PrefUtils.markUserId(WalnutSpashActivity.this, Integer.valueOf(heroInfo.userId));

                        Intent mainIntent = new Intent(WalnutSpashActivity.this, WalnutMainActivity.class);
                        WalnutSpashActivity.this.startActivity(mainIntent);
                    }
                    else {
                        Intent regIntent = new Intent(WalnutSpashActivity.this, WalnutLoginActivity.class);
                        WalnutSpashActivity.this.startActivity(regIntent);
                    }

                    WalnutSpashActivity.this.finish();
                }
            }
        };

        future = scheduler.scheduleAtFixedRate(worker, 0, 2 * SPLASH_PERIOD, TimeUnit.SECONDS);
    }

    @Override
    protected void onDestroy() {
        scheduler = null;
        login_count = 0;
        super.onDestroy();
    }
}
